package com.gps.misc;

import java.util.ArrayList;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.datatypes.ActivityTracks;
import com.datatypes.Friend;
import com.datatypes.Track;
import com.datatypes.User;
import com.datatypes.listItemData;
import com.facebook.android.FBimageManager;
import com.facebook.android.FBimageManagerV2;
import com.sporteamws.R;
import com.sporteamws.SharedPref;

public class ActivityList extends BaseAdapter {

	private Activity activity;
	private ArrayList<listItemData> data;
	private static LayoutInflater inflater = null;

	public ActivityList(Activity a, ArrayList<listItemData> arrayLst) {
		activity = a;
		data = arrayLst;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		FBimageManagerV2.INSTANCE.setPlaceholder(BitmapFactory.decodeResource(
				a.getResources(), R.drawable.profile));
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		listItemData listItem = data.get(position);

		if (listItem.isGhostMode()) {
			if (convertView == null)
				view = inflater.inflate(R.layout.list_row_ghost, null);
			setGhost(view, listItem.getTrack());
		} else if (listItem.isActivityMode()) {
			if (convertView == null)
				view = inflater.inflate(R.layout.list_row_activity, null);
			setActivity(view, listItem.getActivity());
		} else {
			if (convertView == null)
				view = inflater.inflate(R.layout.list_row_friend, null);
			setFriend(view, listItem.getFriend());
		}
		return view;
	}

	private void setFriend(View view, Friend friend) {
		TextView FriendName = (TextView) view.findViewById(R.id.friend_name);
		ImageView img = (ImageView) view.findViewById(R.id.friend_img);

		FriendName.setText(friend.getName());
		img.setTag(friend.getFbID());
		FBimageManagerV2.INSTANCE.loadBitmap(friend.getFbID(), img,
				this.activity);
	}

	private void setActivity(View view, com.datatypes.Activity activity) {

		TextView userName = (TextView) view
				.findViewById(R.id.friend_name_activity);
		TextView distance = (TextView) view
				.findViewById(R.id.distance_activity);
		TextView duration = (TextView) view
				.findViewById(R.id.duration_activity);
		TextView date = (TextView) view.findViewById(R.id.date_activity);
		TextView uTurn = (TextView) view.findViewById(R.id.uTurn_activity);
		ImageView img = (ImageView) view.findViewById(R.id.thumbnail);

		String FriendID = null;

		if (activity.isImTheSender(SharedPref.facebookID)) {
			userName.setText(activity.getReceiverName());
			FriendID = activity.getReceiverID();
			if (activity.getReceiverTrack() == null)
				uTurn.setText("opponent's turn");
			else
				uTurn.setText("opponent response");
		} else {
			userName.setText(activity.getSenderName());
			FriendID = activity.getSenderID();
			uTurn.setText("your turn");
		}
		Track track = null;

		if (activity.getReceiverTrack() == null)
			track = activity.getSenderTrack();
		else
			track = activity.getReceiverTrack();

		distance.setText(makeDistance(track.GetDistance()));
		duration.setText(convertToTime(track.GetTrackTime()));
		date.setText((new Date(track.getDate()).toLocaleString()));

		img.setTag(FriendID);
		FBimageManagerV2.INSTANCE.loadBitmap(FriendID, img, this.activity);

	}

	private void setGhost(View view, Track track) {
		TextView trackName = (TextView) view.findViewById(R.id.track_name);
		TextView distance = (TextView) view.findViewById(R.id.distance_ghost);
		TextView duration = (TextView) view.findViewById(R.id.duration_ghost);
		TextView date = (TextView) view.findViewById(R.id.date_ghost);

		trackName.setText(track.getName());
		distance.setText(makeDistance(track.GetDistance()));
		duration.setText(convertToTime(track.GetTrackTime()));
		date.setText((new Date(track.getDate()).toLocaleString()));

	}

	private String convertToTime(long totalTime) {
		totalTime /= 1000;
		int sec = (int) (totalTime % 60);
		totalTime /= 60;
		int min = (int) (totalTime % 60);
		totalTime /= 60;
		int hour = (int) (totalTime % 60);

		if (hour == 0)
			return "Total time: " + ((min < 10) ? "0" + min : min) + ":"
					+ ((sec < 10) ? "0" + sec : sec);
		else
			return "Total time: " + ((hour < 10) ? "0" + hour : hour) + ":"
					+ ((min < 10) ? "0" + min : min);
	}

	private String makeDistance(double distance) {

		String distanceStr = null;

		if (distance > 1000)
			distanceStr = String.format("%.02f", distance / 1000) + " km";
		else
			distanceStr = String.format("%.02f", distance) + " meters";

		return distanceStr;
	}
}
